<?php

/**
 * @author: biggege
 * @since:  2021-08-11 16:12:34
 */

namespace App\Validations\Manage;

use App\Helpers\ManageValidator;
use App\Models\Area;
use App\Models\Role;
use App\Models\Admin;
use App\Models\Children;
use App\Models\AdminTeam;
use Validator;

trait AccountValidator
{
    use ManageValidator;

    protected $validatorAttributes = [
        'username' => '姓名',
        'gender' => '性别',
        'birthday' => '生日',
        'nation_id' => '民族ID',
        'identity_id' => '身份证号',
        'avatar' => '头像',
        'province' => '省',
        'city' => '市',
        'area' => '区县',
        'town' => '乡镇',
        'village' => '村',
        'family_province' => '家所在的省',
        'family_city' => '家所在的市',
        'family_area' => '家所在的区县',
        'family_address' => '家庭详细地址',
        'profession' => '职业',
        'phone' => '手机号',
        'education' => '学历',
        'major' => '专业',
        'graduated_school' => '毕业院校',
        'graduated_date' => '毕业时间',
        'marriage' => '婚姻状态',
        'family_province' => '家所在的省',
        'family_city' => '家所在的市',
        'family_area' => '家所在的区县',
        'family_address' => '家庭详细地址',
        'population_number' => '家庭人口数',
        'children_number' => '孩子数量',
        'status' => '状态',
        'entry_time' => '入职时间',
        'departure_time' => '离职时间',
        'object_id' => '管辖地',
    ];

    // 更新
    private function updateValidation(array $data) {
        $validator = Validator::make($data, [
            'username' => 'required|string|min:1|max:20',
            'gender' => 'required|integer|min:1|max:2',
            'nation_id' => 'required|integer|min:1001|max:1056',
            'identity_id' => 'required|string|min:18|max:18',
            'avatar' => 'sometimes|nullable|string|min:1|max:128',
            'province' => 'required_unless:role_text,'.Role::CHILD_CARE_COUNSELOR.'|integer',
            'city' => 'required_unless:role_text,'.Role::CHILD_CARE_COUNSELOR.'|integer',
            'area' => 'required_unless:role_text,'.Role::CHILD_CARE_COUNSELOR.'|integer',
            'town' => 'required_if:role_text,'.Role::CHILD_CARE_COUNSELOR.'|integer',
            'village' => 'sometimes|nullable|integer',
            'object_id' => 'required_if:role_text,'.Role::CHILD_CARE_COUNSELOR.','.Role::TOWN_SUPERVISOR.','.Role::TOWN_SUPERVISOR_GROUP_LEADER.'|array',
            'parent_id' => 'required_if:role_text,'.Role::CHILD_CARE_COUNSELOR.'|integer',
            'family_province' => 'sometimes|nullable|integer',
            'family_city' => 'sometimes|nullable|integer',
            'family_area' => 'sometimes|nullable|integer',
            'family_town' => 'sometimes|nullable|integer',
            'family_village' => 'sometimes|nullable|integer',
            'family_address' => 'required|string|min:1|max:100',
            'profession' => 'required|string|min:1|max:50',
            'phone' => 'required|max:16',
            'education' => 'required|integer|min:3001|max:3009',
            'graduated_school' => 'required|string|min:1|max:50',
            'graduated_date' => 'required|date',
            'marriage' => 'required|integer',
            'population_number' => 'required|integer|min:0',
            'children_number' => 'required|integer|min:0',
            'children_list' => 'sometimes|nullable|array',
            'children_list.*.id' => 'sometimes|nullable|min:1',
            'children_list.*.name' => 'required|string|min:1|max:20',
            'children_list.*.birthday' => 'required|date',
            'children_list.*.gender' => 'required|string|min:1|max:2',
            'children_list.*.home_visit' => 'required|string|min:1|max:2',
        ], $this->validatorMessage, $this->validatorAttributes);

        if ($validator->fails()) {
            $errors = $validator->errors()->first(); // all()
            if(
                strpos($errors, 'town_supervisor') != false ||
                strpos($errors, 'child_care_counselor') != false ||
                strpos($errors, 'town_supervisor_group_leader') != false
            ) {
                if(strpos($errors, 'object_id') != false) {
                    $errors = '管辖地不能为空';
                }
            }
            return $this->error(400, $errors); // implode(' | ', $errors)
        }

        if(empty($data['id'])) {
            $admin = Admin::where('phone', $data['phone'])->first();
            if(!empty($admin)) {
                return $this->error(400, '手机号已存在');
            }
        }else{
            $admin = Admin::where('id', $data['id'])->first();
            if(empty($admin)) {
                return $this->error(400, '被编辑人员不存在');
            }
            if($admin['phone'] != $data['phone']) {
                $admin = Admin::where('phone', $data['phone'])->first();
                if(!empty($admin)) {
                    return $this->error(400, '手机号已存在');
                }
            }

            // 管辖村
            if ($data['role_text'] == Role::CHILD_CARE_COUNSELOR) {
                $village = $this->village($data);
                if(!empty($village)) {
                    return $this->error(400, $village.'等村下存在管辖儿童');
                }
            }

            // 管辖镇
            if (in_array($data['role_text'], [Role::TOWN_SUPERVISOR, Role::TOWN_SUPERVISOR_GROUP_LEADER])) {
                $town = $this->town($data);
                if(!empty($town)) {
                    return $this->error(400, $town.'等乡/镇下存在婴幼儿养育指导师管辖村');
                }
            }
        }

        // exit(json_encode($data));
    }

    /**
     * 删除数据校验
     * @param  array  $ids [description]
     * @return [type]      [description]
     */
    private function deleteValidation(array $ids) {
        $childrenTotal = Children::whereIn('care_counselor_id', $ids)->count();
        if($childrenTotal > 0) {
            return $this->error(400, '关联其他数据不能删除');
        }

        $adminTotal = Admin::whereIn('parent_id', $ids)->count();
        if($adminTotal > 0) {
            return $this->error(400, '关联其他数据不能删除');
        }
    }

    /**
     * 村子
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    private function village($data)
    {
        $village = AdminTeam::where(['admin_id' => $data['id'], 'object_type' => AdminTeam::OBJECT_TYPE_VILLAGE])->get()->pluck('object_id');
        if($village->isEmpty()) {
            return false;
        }
        $village = $village->toArray();
        $objectDiff = array_diff($village, $data['object_id']);
        if(empty($objectDiff)) {
            return false;
        }

        $village = Children::where('care_counselor_id', $data['id'])->whereIn('village', $objectDiff)->get()->pluck('village');
        if($village->isEmpty()) {
            return false;
        }

        $village = $village->toArray();
        $village = Area::getArea($village, null, null, 'name');
        return implode(',', $village);
    }

    /**
     * 乡镇
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    private function town($data)
    {
        $town = AdminTeam::where(['admin_id' => $data['id'], 'object_type' => AdminTeam::OBJECT_TYPE_TOWN])->get()->pluck('object_id');
        if($town->isEmpty()) {
            return false;
        }
        $town = $town->toArray();
        $objectDiff = array_diff($town, $data['object_id']);
        if(empty($objectDiff)) {
            return false;
        }
        $town = Admin::where('parent_id', $data['id'])->get()->pluck('town');
        if($town->isEmpty()) {
            return false;
        }

        $town = $town->toArray();
        $town = array_intersect($town, $objectDiff);
        if(empty($town)) {
            return false;
        }

        $town = Area::getArea($town, null, null, 'name');
        return implode(',', $town);
    }












}